import axios from 'axios';
import {
  ADD_NUMBER,
  SUB_NUMBER,
  INCREMENT,
  DECREMENT,
  CHANGE_BANNERS,
  CHANGE_RECOMMEND,
  FETCH_HOME_MULTIDATA
} from './constants.js';

export const addAction = (num) => ({
  type: ADD_NUMBER,
  num
})

export const subAction = (num) => ({
  type: SUB_NUMBER,
  num
})

export const incAction = () => ({
  type: INCREMENT,
})

export const decAction = () => ({
  type: DECREMENT,
})

// 轮播图Action
export const changeBannersAction = (banners) => ({
  type: CHANGE_BANNERS,
  banners
})

// 推荐 Action
export const changeRecommendAction = (recommends) => ({
  type: CHANGE_RECOMMEND,
  recommends
})

// redux-thunk 中定义的action函数
export const getHomeMultidata = (dispatch) => {
  console.log("action");
  axios({
    url: "http://123.207.32.32:8000/home/multidata",
  }).then(res => {
    const data = res.data.data;
    dispatch(changeBannersAction(data.banner.list))
    dispatch(changeRecommendAction(data.recommend.list))
  })
}

// Redux-saga拦截的action
export const fetchHomeMultidataAction = {
  type: FETCH_HOME_MULTIDATA
}
